home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / e_to_l / edsspell / absbuff.pas next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  11.3 KB  |  351 lines

  1. (* ABSBUFF.PAS - Copyright (c) 1995-1996, Eminent Domain Software *)
  2.  
  3. unit AbsBuff;
  4.   {-Abstract buffer manager for EDSSpell component}
  5.   {-also includes native Delphi CustomEdit buffer manager}
  6. interface
  7. uses
  8.   Classes, Controls, Graphics, SysUtils, Forms, StdCtrls,
  9.   WinProcs, WinTypes,
  10.   MemoUtil, SpellGbl, Words;
  11.  
  12. type
  13.   TAbsBuffer = class (TObject)       {abstract buffer manager}
  14.     private
  15.       { Private declarations }
  16.       FSize:       Longint;          {size of buffer}
  17.       FCurPosPtr:  PChar;            {points to current character in buffer}
  18.       FBeginPos:   Longint;          {beginning position of word in buffer}
  19.       FEndPos:     Longint;          {ending position of word in buffer}
  20.       FParent:     TControl;         {parent control, if any}
  21.       FAllNums:    Boolean;          {TRUE if the current word is all numbers}
  22.     protected
  23.       { Protected declarations }
  24.     public
  25.       { Public declarations }
  26.       Buffer:      PBigBuffer;       {pointer to the buffer}
  27.       CurPos:      Longint;          {current location in the buffer}
  28.  
  29.       constructor Create (AParent: TControl);  dynamic;
  30.       destructor  Destroy;  override;
  31.  
  32.       procedure   InitParms;
  33.         {-initializes buffer parameters}
  34.       function    IsModified: Boolean;  virtual;  abstract;
  35.         {-returns TRUE if parent had been modified}
  36.       procedure   SetModified (NowModified: Boolean);  virtual;  abstract;
  37.         {-sets parents modified flag}
  38.       function    GetYPos: integer;  virtual;
  39.         {-gets the current y location of the highlighted word (absolute screen)}
  40.       function    GetNextWord: string;  dynamic;  abstract;
  41.         {-returns the next word in the buffer}
  42.       procedure   MoveBackOneWord;  dynamic;  abstract;
  43.         {-moves back to the beginning of the current word}
  44.       procedure   UpdateBuffer;  dynamic;  abstract;
  45.         {-updates the buffer from the parent component, if any}
  46.       procedure   SetSelectedText;  dynamic;
  47.         {-highlights the current word using FBeginPos & FEndPos}
  48.       procedure   ReplaceWord (WithWord: string);  dynamic;  abstract;
  49.         {-replaces the current word with the word provided}
  50.       procedure   WordCount (var NumWords, UniqueWords: Longint);  dynamic;
  51.         {-returns the number of words in buffer}
  52.  
  53.       { Property declarations }
  54.       property    BufSize: Longint     read  FSize
  55.                                        write FSize;
  56.       property    PCurPos: PChar       read  FCurPosPtr
  57.                                        write FCurPosPtr;
  58.       property    BeginPos: Longint    read  FBeginPos
  59.                                        write FBeginPos;
  60.       property    EndPos: Longint      read  FEndPos
  61.                                        write FEndPos;
  62.       property    Parent: TControl     read  FParent
  63.                                        write FParent;
  64.       property    Modified: Boolean    read  IsModified
  65.                                        write SetModified;
  66.       property    YPos: Integer        read  GetYPos;
  67.       property    AllNumbers: Boolean  read  FAllNums
  68.                                        write FAllNums;
  69.  end; { TAbsBuffer }
  70.  
  71.   TPCharBuffer = class (TAbsBuffer)  {PChar buffer manager}
  72.     private
  73.       { Private declarations }
  74.       FModified: Boolean;            {Internal modified flag}
  75.       FPChar:    PChar;              {Parent Buffer}
  76.     protected
  77.       { Protected declarations }
  78.     public
  79.       { Public declarations }
  80.       constructor Create (AParent: TControl);  override;
  81.       constructor CreateSpecial (ParentBuffer: PChar);  dynamic;
  82.  
  83.       function    IsModified: Boolean;  override;
  84.         {-returns TRUE if parent had been modified}
  85.       procedure   SetModified (NowModified: Boolean);  override;
  86.         {-sets parents modified flag}
  87.       function    GetNextWord: string;  override;
  88.         {-returns the next word in the buffer}
  89.       procedure   MoveBackOneWord;  override;
  90.         {-moves back to the beginning of the current word}
  91.       procedure   UpdateBuffer;  override;
  92.         {-updates the buffer from the parent component, if any}
  93.       procedure   ReplaceWord (WithWord: string);  override;
  94.         {-replaces the current word with the word provided}
  95.   end;  { TPCharBuffer }
  96.  
  97.   TCEBuffer = class (TPCharBuffer)   {TCustomEdit buffer manager}
  98.     private                          {works for TMemo & TEdit}
  99.       { Private declarations }
  100.     protected
  101.       { Protected declarations }
  102.     public
  103.       { Public declarations }
  104.       constructor Create (AParent: TControl);  override;
  105.  
  106.       function    IsModified: Boolean;  override;
  107.         {-returns TRUE if parent had been modified}
  108.       procedure   SetModified (NowModified: Boolean);  override;
  109.         {-sets parents modified flag}
  110.       function    GetYPos: integer;  override;
  111.         {-gets the current y location of the highlighted word (absolute screen)}
  112.       procedure   SetSelectedText;  override;
  113.         {-highlights the current word using BeginPos & EndPos}
  114.       procedure   UpdateBuffer;  override;
  115.         {-updates the buffer from the parent component, if any}
  116.       procedure   ReplaceWord (WithWord: string);  override;
  117.         {-replaces the current word with the word provided}
  118.   end;  { TCEBuffer }
  119.  
  120.  
  121. implementation
  122.  
  123. {Abstract Buffer Manager}
  124. constructor TAbsBuffer.Create (AParent: TControl);
  125. begin
  126.   inherited Create;
  127.   FParent := AParent;
  128.   GetMem (Buffer, MaxBuffer);
  129.   InitParms;
  130.   UpdateBuffer;
  131. end;  { TAbsBuffer.Create }
  132.  
  133. procedure TAbsBuffer.InitParms;
  134.   {-initializes buffer parameters}
  135. begin
  136.   CurPos := 1;
  137.   PCurPos := @Buffer^[1];
  138. end;  { TAbsBuffer.InitParms }
  139.  
  140. function TAbsBuffer.GetYPos: integer;
  141.   {-gets the current y location of the highlighted word (absolute screen)}
  142. begin
  143.   Result := 0;
  144. end;  { TAbsBuffer.GetYPos }
  145.  
  146. procedure TAbsBuffer.WordCount (var NumWords, UniqueWords: Longint);
  147.   {-returns the number of words in buffer}
  148. var
  149.   UniqueList:  TStringList;
  150.   WordSt:      String;
  151.   Words:       String;
  152.   UniqueSt:    String;
  153. begin
  154.   InitParms;
  155.   NumWords     := 0;
  156.   UniqueWords  := 0;
  157.   UniqueList   := TStringList.Create;
  158.   if WordForm = nil then
  159.     WordForm := TWordForm.Create (Application);
  160.   if not WordForm.Visible then WordForm.Show;
  161.   repeat
  162.     WordSt := UpperCase (GetNextWord);
  163.     if WordSt <> '' then
  164.     begin
  165.       Inc (NumWords);
  166.       if UniqueList.IndexOf (WordSt) = (-1) then
  167.         UniqueList.Add (WordSt);
  168.     end;  { if... }
  169.     Str (NumWords, Words);
  170.     Str (UniqueList.Count, UniqueSt);
  171.     WordForm.lblWords.Caption := Words;
  172.     WordForm.lblUniqueWords.Caption := UniqueSt;
  173.     Application.ProcessMessages;
  174.     if not WordForm.Visible then  {form was closed}
  175.     begin
  176.       NumWords := 0;
  177.       UniqueList.Clear;
  178.       Break;
  179.     end;  { if... }
  180.   until WordSt = '';
  181.   UniqueWords := UniqueList.Count;
  182.   UniqueList.Free;
  183. end;  { TAbsBuffer.WordCount }
  184.  
  185. procedure TAbsBuffer.SetSelectedText;
  186. begin
  187.   {do nothing}
  188. end; { TabsBuffer.SetSelectedText }
  189.  
  190. destructor TAbsBuffer.Destroy;
  191. begin
  192.   if Buffer <> nil then
  193.     FreeMem (Buffer, MaxBuffer);
  194.   inherited Destroy;
  195. end;  { TAbsBuffer.Destroy }
  196.  
  197. {PChar Buffer Manager}
  198. constructor TPCharBuffer.Create (AParent: TControl);
  199. begin
  200.   inherited Create (AParent);
  201.   FModified := FALSE;
  202. end;  { TPCharBuffer.Create }
  203.  
  204. constructor TPCharBuffer.CreateSpecial (ParentBuffer: PChar);
  205. begin
  206.   inherited Create (nil);
  207.   FPChar := ParentBuffer;
  208.   FModified := FALSE;
  209. end;  { TPCharBuffer.CreateSpecial }
  210.  
  211. function TPCharBuffer.IsModified: Boolean;
  212.   {-returns TRUE if parent had been modified}
  213. begin
  214.   Result := FModified;
  215. end;  { TPCharBuffer.IsModified }
  216.  
  217. procedure TPCharBuffer.SetModified (NowModified: Boolean);
  218.   {-sets parents modified flag}
  219. begin
  220.   FModified := NowModified;
  221. end;  { TPCharBuffer.SetModified }
  222.  
  223. function TPCharBuffer.GetNextWord: string;
  224.   {-returns the next word in the buffer}
  225. var
  226.   S: string;  {string being constructed}
  227. begin
  228.   BeginPos := CurPos;
  229.   EndPos   := CurPos;
  230.   S        := '';
  231.   FAllNums := TRUE;
  232.   {find the first letter of the next word}
  233.   while (not (Char (PCurPos^) in ValidChars)) and
  234.         (CurPos<BufSize) do
  235.   begin
  236.     Inc (CurPos, 1);
  237.     PCurPos := @Buffer^[CurPos];
  238.   end; {  while }
  239.   if CurPos<BufSize then
  240.   begin
  241.     BeginPos := CurPos;
  242.     {goto the end of the word}
  243.     while ((Char (PCurPos^) in ValidChars) and
  244.            (CurPos<BufSize)) do
  245.     begin
  246.       S := ConCat (S, Char (PCurPos^));
  247.       Inc (CurPos, 1);
  248.       if FAllNums and (not (Char (PCurPos^) in NumberSet)) then
  249.         FAllNums := FALSE;
  250.       PCurPos := @Buffer^[CurPos];
  251.       EndPos := CurPos;
  252.     end;  { while }
  253.     Result := S;
  254.   end {:} else
  255.     Result := '';
  256. end;  { TPCharBuffer.GetNextword }
  257.  
  258. procedure TPCharBuffer.MoveBackOneWord;
  259.   {-moves back to the beginning of the current word}
  260. begin
  261.   while (Char (PCurPos^) in ValidChars) and (CurPos > 1) do
  262.   begin
  263.     Dec (CurPos, 1);
  264.     PCurPos := @Buffer^[CurPos];
  265.   end; {  while }
  266. end;  { TPCharBuffer.MoveBackOneWord }
  267.  
  268. procedure TPCharBuffer.UpdateBuffer;
  269.   {-updates the buffer from the parent component, if any}
  270. begin
  271.   BufSize := StrLen (FPChar) + 1;
  272.   Move (FPChar^, Buffer^, BufSize);
  273.   PCurPos := @Buffer^[CurPos];
  274. end;  { TPCharBuffer.UpdateBuffer }
  275.  
  276. procedure TPCharBuffer.ReplaceWord (WithWord: string);
  277.   {-replaces the current word with the word provided}
  278. begin
  279.   {Delete the current words}
  280.   {Insert the new one}
  281.   UpdateBuffer;
  282. end;  { TPCharBuffer.ReplaceWord }
  283.  
  284. {TCustomEdit Buffer Manager}
  285. constructor TCEBuffer.Create (AParent: TControl);
  286. begin
  287.   inherited Create (AParent);
  288. end;  { TCEBuffer.Create }
  289.  
  290. function TCEBuffer.IsModified: Boolean;
  291.   {-returns TRUE if parent had been modified}
  292. begin
  293.   Result := TCustomEdit (Parent).Modified;
  294. end;  { TCEBuffer.IsModified }
  295.  
  296. procedure TCEBuffer.SetModified (NowModified: Boolean);
  297.   {-sets parents modified flag}
  298. begin
  299.   TCustomEdit (Parent).Modified := Modified;
  300. end;  { TCEBuffer.SetModified }
  301.  
  302. function TCEBuffer.GetYPos: integer;
  303.   {-gets the current y location of the highlighted word (absolute screen)}
  304. var
  305.   AbsMemoXY: TPoint;
  306. begin
  307.   if Parent is TMemo then
  308.   begin
  309.     Result := Memo_WhereY (TMemo (Parent));
  310.   end {:} else
  311.   begin
  312.     AbsMemoXY := Parent.ClientToScreen (Point (0, 0));
  313.     Result := AbsMemoXY.Y;
  314.   end;  { else }
  315. end;  { TCEBuffer.GetYPos }
  316.  
  317. procedure TCEBuffer.SetSelectedText;
  318.   {-highlights the current word using BeginPos & EndPos}
  319. begin
  320.   with Parent as TCustomEdit do
  321.   begin
  322.     SelStart  := BeginPos - 1;
  323.     SelLength := EndPos - BeginPos;
  324.     Update;
  325.   end;  { with }
  326. end;  { TCEBuffer.SetSelectedText }
  327.  
  328. procedure TCEBuffer.UpdateBuffer;
  329.   {-updates the buffer from the parent component, if any}
  330. begin
  331.   BufSize := TCustomEdit (Parent).GetTextLen + 1;
  332.   TCustomEdit (Parent).GetTextBuf (pChar(Buffer), BufSize);
  333.   {support international characters}
  334.   AnsiToOemBuff (pChar (Buffer), pChar (Buffer), BufSize);
  335.   PCurPos := @Buffer^[CurPos];
  336. end;  { TCEBuffer.UpdateBuffer }
  337.  
  338. procedure TCEBuffer.ReplaceWord (WithWord: string);
  339.   {-replaces the current word with the word provided}
  340. begin
  341.   with Parent as TCustomEdit do
  342.   begin
  343.     CurPos   := CurPos - (EndPos - BeginPos);
  344.     SelText  := WithWord;
  345.     CurPos   := CurPos + SelLength;
  346.     UpdateBuffer;
  347.   end;  { with }
  348. end;  { TCEBuffer.ReplaceWord }
  349.  
  350. end.  { AbsBuff }
  351.